var app = getApp();
let constants = require('../../common/Constants.js');
let network_utils = require('../../http/NetworkUtils.js');
import * as stateUtils from '../../model/StateUtils';
import * as umfToast from '../../utils/UmfToast';
import * as UmfDialog from '../../utils/UmfDialog';
 
var DeployingList = {
  /**
     * 处理点击事件--删除商户
     * @param e
     * saas四期
     */
  deleteStore: function (e) {
    let noticeMsg = "请注意，删除商户后将无法恢复，是否确认删除" + e.target.dataset.current + "的商户"
    UmfDialog.show(noticeMsg, () => {
      let params = {
        storeId: e.target.id
      };
      network_utils.buildRequest(constants.Interfaces.FUNC_DELETE_STOREINFO, params, {
        doHttpSucess: (response) => {
          if (constants.RetCodes.SUCCESS === response.retCode) {
            umfToast.showToast("删除成功");
            this.onRefresh();
          }
          return false
        }
      }).send();
    }, true);
  },
  gotoDetail:function(e){
    let storeId = e.currentTarget.dataset.storeid
    if (this.data.currtabIndex==2){
      return
    }
    wx:wx.navigateTo({
      url: '/pages/selfhelpDeployment/deploymentDetails/deploymentDetails?storeId=' + storeId + '&fromType=' + e.currentTarget.dataset.type,
    })
    // url = "{{url}}?storeId={{item.storeId}}&fromType={{type}}"
  },
  /**
     * pos 修改或者查看点击事件
     * @param e
     */
  posRevise:function(e){
    let storeInfo = e.currentTarget.dataset.storeinfo;
    let isQuery = storeInfo.posState != '-2'
    wx.navigateTo({
      //TODO  isStore的值网络请求获取
      url: '/pages/basisInfo/openPos/openPos?storeId=' + storeInfo.storeId + '&posFrom=selfDeploy&merIdBb=' + storeInfo.merIdBb+'&isQuery=' + isQuery+'&isStore=true',
    })
  },
    /**
     * 处理点击事件
     * @param e
     */
    doProcess: function (e) {
        let storeInfo = e.currentTarget.dataset.storeinfo;
        let storeId = storeInfo.storeId;
        let stateCenter = storeInfo.stateCenter;
        if (stateCenter.isPos) {
            //处理POS的场合
            wx.navigateTo({
                url: "/pages/selfhelpDeployment/deploymentDetails/deploymentDetails?storeId=" + storeId + "&fromType=agentList"
            });
            return;
        }

        if (stateCenter.isInviting) {
            //待邀请商户
            network_utils.buildRequest(constants.Interfaces.FUNC_QRCODE_DISPLAY, {storeId: storeId}, {
                doHttpSucess: (response) => {
                    if (constants.RetCodes.SUCCESS === response.retCode) {
                        this._jumpTo(Object.assign(response, storeInfo), stateCenter);
                        return true;
                    }
                    return false;
                },
            }).send();
            return;
        }

        //统一请求详情
        network_utils.buildRequest(constants.Interfaces.FUNC_MERCHANT_DETAIL, {storeId: storeId}, {
            doHttpSucess: (response) => {
                if (constants.RetCodes.SUCCESS === response.retCode) {
                    this._jumpTo(response, stateCenter);
                    return true;
                }
                return false;
            },
        }).send();
    },

    /**
     * 跳转的页面可能用到详情数据，需要带详情判断逻辑
     * @param storeDetails
     * @param stateCenter
     * @private
     */
    _jumpTo(storeDetails, stateCenter) {
        if (stateCenter.isStoreing) {
            //待提交店铺信息
            wx.navigateTo({
                url: '/pages/basisInfo/basisInfo?index=1' + '&storeId=' + storeDetails.storeId,
            });
        } else if (stateCenter.isPhotoing) {
            //待上传照片
            let {storeId, payeeName, storeMan, storeType, isAuthorize, payeeCardPhotoFrontUrl, payeeCardPhotoConUrl, categoryCardPhotoFrontUrl,
                categoryCardPhotoCon, shopSignFrontUrl, shopInUrl, storeLicenseNoUrl, authCertUrl
            } = storeDetails;
            let paramsJson = JSON.stringify({
                storeId: storeId,
                payeeName: payeeName,
                storeMan: storeMan,
                storeType: storeType,
                isAuthorize: isAuthorize,
                payeeCardPhotoFront: encodeURIComponent(payeeCardPhotoFrontUrl),
                payeeCardPhotoCon: encodeURIComponent(payeeCardPhotoConUrl),
                categoryCardPhotoFront: encodeURIComponent(categoryCardPhotoFrontUrl),
                categoryCardPhotoCon: encodeURIComponent(categoryCardPhotoCon),
                shopSignPhotoFront: encodeURIComponent(shopSignFrontUrl),
                shopSignPhotoCon: encodeURIComponent(shopInUrl),
                busiLicensePhoto: encodeURIComponent(storeLicenseNoUrl),
                authCertPhoto: encodeURIComponent(authCertUrl)
            });
 
            wx.navigateTo({
                url: '/pages/basisInfo/basisInfo?index=2' +
                '&storeId=' + storeId + '&isPos=' + storeDetails.isPos + '&storeAddress=' + storeDetails.storeAddress+
                '&paramsJson=' + paramsJson,
            });
        } else if (stateCenter.isProducting) {
            //待开通产品
            wx.navigateTo({
              url: '/pages/basisInfo/basisInfo?index=3' + '&storeId=' + storeDetails.storeId + '&storeAddress=' + storeDetails.storeAddress+'&isPos='+storeDetails.isPos ,
            });
        } else if (stateCenter.isInviting) {
            //待邀请商户
            let jsonData = {
                from: "progress",
                storeId: storeDetails.storeId,
                storeShortName: storeDetails.storeShortName,
                payeeName: storeDetails.payeeName,
                qrUrl: encodeURIComponent(storeDetails.qrCodeUrl)
            };
            wx.navigateTo({
                url: '/pages/basisInfo/inviteMerchants/inviteMerchants?jsonData=' + JSON.stringify(jsonData),
            })
        } else if (stateCenter.invitingDone//待修改
          // stateCenter.isModifying //待修改
            || stateCenter.isChecking //待复查
            || stateCenter.isSigning //待签约
            || stateCenter.isReviewing //待审核
            || stateCenter.isReporting //待报备
            || stateCenter.isConfigRelation) { //待配置银商关系
            wx.navigateTo({
                url: '/pages/selfhelpDeployment/deploymentDetails/deploymentDetails?storeId=' + storeDetails.storeId + "&fromType=agentList"
            })
        }
    },

    /**
     * 加载更多事件
     */
    loadMore: function () {
        this._requestList();
    },

    /**
     * 请求成功
     * @param response
     * @private
     */
    _requestSuccess: function (response) {
        let subList = response["storeList"];
        let filterList = this._convertList(subList);
        let hasMore = subList.length === parseInt(app.globalData.pageSize);

        this.setData({
            storeList: this.pageNo === "1" ? filterList : this.data.storeList.concat(filterList),
            hasMore: hasMore
        });

        if (!hasMore) {
            umfToast.showToast("已经加载完所有数据");
        } else {
            //成功并且有更多的场合pageNo+1
            this.pageNo = (parseInt(this.pageNo) + 1).toString();
        }
    },

    _convertList(subList) {
      let that=this
        let helpers = {
            filterData: function (storeInfo) {
              let storeState = new stateUtils.registProcssState(storeInfo.agentcheckState?true:false,storeInfo.state ? storeInfo.state : storeInfo.agentcheckState);
                return !!storeState.code;
            },

            mapList: function (storeInfo) {
              storeInfo.stateCenter = new stateUtils.registProcssState(storeInfo.agentcheckState ? true :false,storeInfo.state ? storeInfo.state : storeInfo.agentcheckState);
                if (storeInfo.stateCenter.isImPay) {
                    storeInfo.doProcess = 'doProcess';
                }
                return storeInfo;
            }
        };
        subList = subList.filter(helpers.filterData)
            .map(helpers.mapList);

        return subList;
    }
};

module.exports = DeployingList;

